(function(){
    "use strict";
    var onlineDeliveryController=function ($scope,$http,$modal,online_deliveryService,objectIdService,online_testService,projectstart_Service){

        //获取所有全局变量的值,并加载页面数据
        $scope.rootParasGet1 = function (){
            $scope.projectId = objectIdService('0');
            $scope.load1();
            $scope.load2();
        };

        //获取项目的所有产品的版本号信息(包括每个版本的下载地址)
        $scope.prosVersionGet = function (){
            var para = {
                projectId : $scope.projectId
            };
            online_deliveryService.getProductForDown(para).success(function(data){
                if(data.result != null){
                    $scope.prosInProject = data.result;
                    angular.forEach($scope.prosInProject,function (item) {
                        item.showList = [];
                        angular.forEach(item.versionList,function (version) {
                            var showBean = {
                                version:version
                            };
                            item.showList.push(showBean);
                        });
                    });
                }
            });
        };

        //获取某个项目的涉及人员信息（pm,dm,tester等人员姓名、联系方式以及项目名称）
        $scope.personsinfoGet = function (){
            var para = {
                projectId : $scope.projectId
            };
            projectstart_Service.getProject(para).success(function(data){
                if(data.result != null){
                    $scope.personsInfo = data.result;
                    $scope.projectName = $scope.personsInfo.projectName;
                }
            });
        };

        //项目下所有产品的版本信息
        $scope.prosInProject = [];
        //项目下的涉及人员信息
        $scope.personsInfo = {};
        //第一个tab页面的初始化方法
        $scope.load1 = function (){
            //获取该项目下所有产品的版本信息
           $scope.prosVersionGet();
           $scope.personsinfoGet();
        };

        //下载部署包到本地，并将下载记录保存到数据库（后台实现）
        $scope.download = function (){
            var param = {
                downloadDirs : []
            };
            var projectId = $scope.projectId;
            var selectList = $scope.prosInProject;

            //数据提交
            var record = {
                projectId : projectId,
                downloadedProducts : ""
            };
            angular.forEach(selectList,function (item) {
                angular.forEach(item.showList,function (show) {
                    if(item.selectVersion == null && show.state == true){
                        item.selectVersion = show.version;
                    }
                });
                if(item.selectVersion != null){
                    var product = {
                        id:item.bdfProduct.id,
                        type:"2",
                        version:item.selectVersion,
                        path:$scope.path
                    };
                    param.downloadDirs.push(product);
                    record.downloadedProducts += ""+item.productName +":"+item.selectVersion+";";
                }
            });
            if(param.downloadDirs.length == 0){
                $scope.loading = false;
                $.Huimodalalert("未选择产品或套件版本，请检查！",1000);
                return;
            }
            var length = record.downloadedProducts.lastIndexOf(";");
            record.downloadedProducts = record.downloadedProducts.substr(0,length);

            online_deliveryService.downloadtarfile(param).success(function(data){
                if(data.errorMsg != null){
                    alert(data.errorMsg,1000);
                } else {
                    var fileName = data.fileName;
                    online_deliveryService.savedownloadhistoryrecords(record).success(function () {
                        $scope.loading = false;
                        if(data.errorMsg != null){
                            $.Huimodalalert(data.errorMsg,1000);
                        } else {
                            //特殊字符+，&转义处理
                            fileName = fileName.replace(/\+/g, "%2B");
                            fileName = fileName.replace(/\&/g, "%26");
                            window.location.href = "/api/file/download?type=1&fileName=" + fileName;
                        }
                    });
                }
            });
        };

        //上线支付页面（历史记录子页面）中显示该项目部署的tar包历史下载记录
        $scope.downloadhistoryrecordsGet = function (){
            var result = [];
            var para = {
                projectId : $scope.projectId
            };
            online_deliveryService.showdownloadhistoryrecords(para).success(function(data){
                if(data.result != null){
                    $scope.downloadHistoryRecords = data.result;
                    angular.forEach($scope.downloadHistoryRecords,function (item) {
                       if (item.downloadedProducts != null){
                           var productVersions = item.downloadedProducts.split(";");
                           item.proVersionInfoList = [];
                           angular.forEach(productVersions,function (info) {
                               item.proVersionInfoList.push(info);
                           });
                       };
                    });
                }
            });
            return result;
        };

        //用来存储tar包下载历史记录
        $scope.downloadHistoryRecords = [];
        //第二个tab页面的加载方法
        $scope.load2 = function (){
            $scope.personsInfo = $scope.personsinfoGet();
            $scope.downloadhistoryrecordsGet();
        };
    };

    angular.module("cicd.onlinedelivery",[])
           .controller("OnlineDeliveryController",["$scope","$http","$modal","online_deliveryService",
                "objectIdService","online_testService","projectstart_Service",onlineDeliveryController])

})();